<!--
产品管理—主页
凌辉
2017-5-25
-->
<template>
  <el-row id="product">
    <div class="oh mb10">
      <el-button type="info" v-if="pageType !== 'look'&&loginType !== 0" size="small" @click="handleAdd">+新增价格/库存
      </el-button>
      <el-button type="warning" v-if="pageType !== 'look'&&loginType !== 0" size="small" @click="handleEditAll">修改库存
      </el-button>
      <el-button type="danger" v-if="pageType !== 'look'&&loginType !== 0" :disabled="this.sels.length===0" size="small"
                 @click="batchDelete">批量删除
      </el-button>
      <el-button type="info" size="small" @click="handleLog">操作日志</el-button>
    </div>
    <div>
      <div id="tab_rl">
        <div class="fl f14 fb ml20" style="line-height:32px">
          <img src="../images/arrow_l.png" style="width:10px" class="hand" @click="yearAddSub(-1)"><span
          class="ml10 mr10">{{yearStr}}年</span>
          <img src="../images/arrow_r.png" style="width:10px" class="hand" @click="yearAddSub(1)">
        </div>
        <ul class="tab_menu oh ">
          <li class="others" id="_1" @click="weekNum(yearStr,1)">1月</li>
          <li class="others" id="_2" @click="weekNum(yearStr,2)">2月</li>
          <li class="others" id="_3" @click="weekNum(yearStr,3)">3月</li>
          <li class="others" id="_4" @click="weekNum(yearStr,4)">4月</li>
          <li class="others" id="_5" @click="weekNum(yearStr,5)">5月</li>
          <li class="others" id="_6" @click="weekNum(yearStr,6)">6月</li>
          <li class="current" id="_7" @click="weekNum(yearStr,7)">7月</li>
          <li class="others" id="_8" @click="weekNum(yearStr,8)">8月</li>
          <li class="others" id="_9" @click="weekNum(yearStr,9)">9月</li>
          <li class="others" id="_10" @click="weekNum(yearStr,10)">10月</li>
          <li class="others" id="_11" @click="weekNum(yearStr,11)">11月</li>
          <li class="others" id="_12" @click="weekNum(yearStr,12)">12月</li>
        </ul>
      </div>
      <el-table border :data="dataList" style="width: 100%; font-size: 12px;" @selection-change="selsChange"
                row-class-name="tabRow">
        <el-table-column type="selection" :selectable="selectablePop"></el-table-column>
        <el-table-column align="center" prop="month" :label="monthStr + '月'"></el-table-column>
        <el-table-column align="center" prop="aduMan" label="成人价格"></el-table-column>
        <el-table-column align="center" prop="chdMan" label="儿童价格"></el-table-column>
        <el-table-column align="center" prop="room" label="单房差"></el-table-column>
        <el-table-column align="center" :formatter="inventoryNumStr" label="库存数"></el-table-column>
        <el-table-column align="center" :formatter="oldInventoryNumStr" label="已售库存"></el-table-column>
        <el-table-column align="center" prop="isBeyond" label="是否超售" width="100"
                         show-overflow-tooltip></el-table-column>
        <el-table-column align="center" v-if="pageType !== 'look'&&loginType !== 0" width="100" label="操作">
          <template scope="scope">
            <el-button size="small" type="warning" v-if="scope.row.id > 0" @click="handleEdit(scope.$index, scope.row)">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--新增价格/库存弹窗-->
      <el-dialog :title="dialogAddTitle" size="large" style="width: 800px;margin:0 auto;overflow:hidden"
                 v-model="showFormAdd" v-if="showFormAdd" :close-on-click-modal="false"
                 :before-close="resetStock" top="10%">
        <div class="mt10" id="tab">
          <ul class="tab_menu oh " style="margin-bottom: 10px;">
            <li @click="dateButtonCss(false)" :class="[dateButton?'current':'others']">按日期
            </li>
            <li @click="dateButtonCss(true)" :class="[dateButton?'others':'current']">按周期</li>
          </ul>
          <!--按日期-->
          <div class="tab_contents" v-if="!dateButton">
            <div class="graybg" style="background-color:#fff">
              <div class="flex">
                <div>
                  <calendar ref="calend" :range="calendarInfo.range" :lunar="calendarInfo.lunar"
                            @selectM="selectM"></calendar>
                </div>
                <div style="width: 30%;height: 240px;text-align: center;float: left">
                  <p>选中的日期</p>
                  <select id="sel" v-model="selValue" class="selDate" name="specDates" multiple="multiple">
                    <option value=""></option>
                  </select>
                  <div>
                    <el-button @click="selectTourDel" type="danger" size="small" style="padding: 3px 9px;">删除
                    </el-button>
                  </div>
                </div>
              </div>
            </div>
            <table class="itable" cellspacing="0" cellpadding="0" border="0" align="center">
              <tr class="itb1">
                <td>价格类型</td>
                <td>结算价</td>
                <td>销售价</td>
                <td>库存数</td>
                <td>是否可超售</td>
                <td v-if="dialogAddTitle !== '新增价格/库存'">已售库存</td>
              </tr>
              <tr>
                <td style="border: 1px solid #f0f0f0;">成人价格</td>
                <td style="border: 1px solid #f0f0f0;">
                  <el-input-number type="number" :controls="false" :min="0"
                                   v-model="dateStockEntitty.adultSettlementPrice"
                                   style="width: 80px;margin-top:5px"></el-input-number>
                </td>
                <td style="border: 1px solid #f0f0f0;">
                  <el-input-number type="number" :controls="false" :min="0" style="width: 80px;margin-top:5px"
                                   v-model="dateStockEntitty.adultSalePrice"></el-input-number>
                </td>
                <td rowspan="3"><input type="number" :min="0" v-model="dateStockEntitty.stock" style="height: 23px;"
                                       class="seccss"></td>
                <td rowspan="3"><select class="seccss" style="height: 23px;" v-model="dateStockEntitty.isOverbooking">
                  <option value="false">否</option>
                  <option value="true">是</option>
                </select></td>
                <td rowspan="3" v-if="dialogAddTitle !== '新增价格/库存'">{{dateStockEntitty.saleStock}}</td>
              </tr>
              <tr class="">
                <td style="border: 1px solid #f0f0f0;">儿童价格</td>
                <td style="border: 1px solid #f0f0f0;">
                  <el-input-number type="number" style="width: 80px;margin-top:5px" :controls="false" :min="0"
                                   v-model="dateStockEntitty.childSettlementPrice"></el-input-number>
                </td>
                <td style="border: 1px solid #f0f0f0;">
                  <el-input-number type="number" style="width: 80px;margin-top:5px" :controls="false" :min="0"
                                   v-model="dateStockEntitty.childSalePrice"></el-input-number>
                </td>
              </tr>
              <tr class="">
                <td style="border: 1px solid #f0f0f0;">单房差</td>
                <td style="border: 1px solid #f0f0f0;">
                  <el-input-number type="number" :controls="false" style="width: 80px;margin-top:5px" :min="0"
                                   v-model="dateStockEntitty.differSettlementPrice"></el-input-number>
                </td>
                <td style="border: 1px solid #f0f0f0;">
                  <el-input-number type="number" style="width: 80px;margin-top:5px" :controls="false" :min="0"
                                   v-model="dateStockEntitty.differSalePrice"></el-input-number>
                </td>
              </tr>
            </table>
            <div class="mt20 oh" style="width:234px;margin:20px auto">
              <div class="qxbtn fl mr30" @click="resetStock">取消</div>
              <div class="ztbtn fl" @click="handSubSaveMoreDate" :loading="saveStockLoading">保存</div>
            </div>
          </div>
          <!--按周-->
          <div class="tab_contents" v-if="dateButton">
            <div>
              <div>
                <el-date-picker
                  v-model="timeRange"
                  type="daterange"
                  :picker-options="pickerOptions0"
                  placeholder="选择日期范围">
                </el-date-picker>
              </div>
              <div class="mt10">
                <span class="mr10">适用日期：</span>
                <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选
                </el-checkbox>
                <div style="margin: 15px 0;"></div>
                <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
                  <el-checkbox v-for="(city,index) in week" :label="city" :key="index">{{city}}</el-checkbox>
                </el-checkbox-group>
              </div>
            </div>
            <table class="itable" style="margin-top: 10px;margin-left: 0px;" cellspacing="0" cellpadding="0" border="0"
                   align="center">
              <tr class="itb1">
                <td>价格类型</td>
                <td>结算价</td>
                <td>销售价</td>
                <td>库存数</td>
                <td>是否可超售</td>
                <td v-if="dialogAddTitle !== '新增价格/库存'">已售库存</td>
              </tr>
              <tr>
                <td style="border: 1px solid #f0f0f0;">成人价格</td>
                <td style="border: 1px solid #f0f0f0;">
                  <el-input-number type="number" :controls="false" style="width: 80px;margin-top:5px" :min="0"
                                   v-model="weekStockEntitty.adultSettlementPrice"></el-input-number>
                </td>
                <td style="border: 1px solid #f0f0f0;">
                  <el-input-number type="number" :controls="false" style="width: 80px;margin-top:5px" :min="0"
                                   v-model="weekStockEntitty.adultSalePrice"></el-input-number>
                </td>
                <td rowspan="3"><input type="number" :controls="false" :min="0" v-model="weekStockEntitty.stock"
                                       class="seccss" style="height: 25px;"></td>
                <td rowspan="3"><select class="seccss" style="height: 25px;" v-model="weekStockEntitty.isOverbooking">
                  <option value="false">否</option>
                  <option value="true">是</option>
                </select></td>
                <td rowspan="3" v-if="dialogAddTitle !== '新增价格/库存'">{{weekStockEntitty.saleStock}}</td>
              </tr>
              <tr class="">
                <td style="border: 1px solid #f0f0f0;">儿童价格</td>
                <td style="border: 1px solid #f0f0f0;">
                  <el-input-number type="number" style="width: 80px;margin-top:5px" :controls="false" :min="0"
                                   v-model="weekStockEntitty.childSettlementPrice"></el-input-number>
                </td>
                <td style="border: 1px solid #f0f0f0;">
                  <el-input-number type="number" style="width: 80px;margin-top:5px" :controls="false" :min="0"
                                   v-model="weekStockEntitty.childSalePrice"></el-input-number>
                </td>
              </tr>
              <tr class="">
                <td style="border: 1px solid #f0f0f0;">单房差</td>
                <td style="border: 1px solid #f0f0f0;">
                  <el-input-number type="number" style="width: 80px;margin-top:5px" :controls="false" :min="0"
                                   v-model="weekStockEntitty.differSettlementPrice"></el-input-number>
                </td>
                <td style="border: 1px solid #f0f0f0;">
                  <el-input-number type="number" style="width: 80px;margin-top:5px" :controls="false" :min="0"
                                   v-model="weekStockEntitty.differSalePrice"></el-input-number>
                </td>
              </tr>
            </table>
            <div class="mt20 oh" style="width:234px;margin:20px auto">
              <div class="qxbtn fl mr30" @click="resetStock">取消</div>
              <div class="ztbtn fl" @click="info" :loading="saveStockLoading">保存</div>
            </div>
          </div>
        </div>
      </el-dialog>
      <!--编辑库存弹窗-->
      <el-dialog :title="dialogEditTitle" size="large" style="width: 800px;margin:0 auto;overflow:hidden"
                 v-model="showFormEdit" :close-on-click-modal="false" :before-close="resetStock" top="10%">
        <div class="mt20 f14">当前日期：<span class="ml10">{{stockEntitty.date}}</span></div>
        <table class="itable mt30" cellspacing="0" cellpadding="0" border="0" align="center">
          <tr class="itb1">
            <td>价格类型</td>
            <td>结算价</td>
            <td>销售价</td>
            <td>库存数</td>
            <td>是否可超售</td>
            <td>已售库存</td>
          </tr>
          <tr>
            <td style="border: 1px solid #f0f0f0;">成人价格</td>
            <td style="border: 1px solid #f0f0f0;">
              <el-input-number type="number" style="width: 80px;margin-top:5px" :controls="false" :min="0"
                               v-model="stockEntitty.adultSettlementPrice"></el-input-number>
            </td>
            <td style="border: 1px solid #f0f0f0;">
              <el-input-number type="number" style="width: 80px;margin-top:5px" :controls="false" :min="0"
                               v-model="stockEntitty.adultSalePrice"
              ></el-input-number>
            </td>
            <td rowspan="3"><input type="number" :min="0" v-model="stockEntitty.stock" class="seccss"
                                   style="height: 25px;"></td>
            <td rowspan="3"><select class="seccss" v-model="stockEntitty.isOverbooking" style="height: 25px;">
              <option value="false">否</option>
              <option value="true">是</option>
            </select></td>
            <td rowspan="3">{{stockEntitty.saleStock}}</td>
          </tr>
          <tr>
            <td style="border: 1px solid #f0f0f0;">儿童价格</td>
            <td style="border: 1px solid #f0f0f0;">
              <el-input-number type="number" style="width: 80px;margin-top:5px" :controls="false" :min="0"
                               v-model="stockEntitty.childSettlementPrice"></el-input-number>
            </td>
            <td style="border: 1px solid #f0f0f0;">
              <el-input-number type="number" style="width: 80px;margin-top:5px" :controls="false" :min="0"
                               v-model="stockEntitty.childSalePrice"></el-input-number>
            </td>
          </tr>
          <tr>
            <td style="border: 1px solid #f0f0f0;">单房差</td>
            <td style="border: 1px solid #f0f0f0;">
              <el-input-number type="number" style="width: 80px;margin-top:5px" :controls="false" :min="0"
                               v-model="stockEntitty.differSettlementPrice"></el-input-number>
            </td>
            <td style="border: 1px solid #f0f0f0;">
              <el-input-number type="number" style="width: 80px;margin-top:5px" :controls="false" :min="0"
                               v-model="stockEntitty.differSalePrice"></el-input-number>
            </td>
          </tr>
        </table>
        <div class="mt20 oh" style="width:234px;margin:20px auto">
          <div class="qxbtn fl mr30" @click="resetStock">取消</div>
          <div class="ztbtn fl" @click="handSubSave" :loading="saveStockLoading">保存</div>
        </div>
      </el-dialog>
      <!--操作日志弹窗-->
      <el-dialog :title="dialogLogTitle" size="large" style="width: 1000px;margin:0 auto;overflow:hidden"
                 v-model="showFormLog" v-if="showFormLog" :close-on-click-modal="false" top="10%">
        <el-table border :data="dataLogList" v-loading="listLogLoading" style="width: 100%; font-size: 12px;"
                  row-class-name="tabRow">
          <el-table-column align="center" width="160" prop="author" label="操作人"></el-table-column>
          <el-table-column align="center" width="160" :formatter="createTimeStr" label="操作时间"></el-table-column>
          <el-table-column align="center" prop="message" show-overflow-tooltip label="操作内容"></el-table-column>
        </el-table>
        <el-col :span="24" class="toolbar" style="margin-top: 15px;margin-bottom: 20px;" v-show="!listLogLoading">
          <el-pagination id="pagination" layout="total, sizes, prev, pager, next, jumper" :page-sizes="[10, 20, 30]"
                         @size-change="handleSizeChange"
                         @current-change="handleCurrentChange" :page-size="pageSize" :total="total"
                         style="float:right;">
          </el-pagination>
        </el-col>
      </el-dialog>
      <div class="black_h" style="display: none;"></div>
    </div>
  </el-row>
</template>

<script>
  import $ from 'jquery'
  import calendar from './calendar.vue'

  $(function () {
    $('.el-picker-panel .el-date-range-picker').style = 'width: 220px; transform-origin: center top 0px; z-index: 2014; position: fixed; top: 124px; left: 372px;'
  })
  export default {
    props: {
      productIdConfig: {
        type: Number
      },
      pageType: {
        type: String
      }
    },
    data () {
      return {
        saveStockLoading: false,
        pickerOptions0: {
          disabledDate (time) {
            return time.getTime() < Date.now() - 8.64e7
          }
        },
        calendarInfo: {
          range: true,
          lunar: true // 显示农历
        },
        sels: [], // 批量操作列表
        checkAll: '',
        isIndeterminate: '',
        checkedCities: [],
        monthStr: '',
        yearStr: '',
        dataList: [],
        timeRange: [],
        week: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
        selValue: [],
        dataInfo: {
          month: '',
          aduMan: '',
          chdMan: '',
          room: '',
          inventoryNum: '',
          oldInventoryNum: '',
          isBeyond: ''
        },
        showFormLog: false,
        showFormEdit: false,
        showFormAdd: false,
        listLogLoading: false,
        dateButton: false,
        total: 2,
        page: 1,
        pageSize: 10,
        dialogLogTitle: '操作日志',
        stockList: [],
        dialogEditTitle: '编辑库存',
        dialogAddTitle: '新增价格/库存',
        stockEntitty: {
          id: '',
          productId: '',
          date: '',
          adultSalePrice: '',
          adultSettlementPrice: '',
          childSalePrice: '',
          childSettlementPrice: '',
          differSalePrice: '',
          differSettlementPrice: '',
          stock: 0,
          saleStock: '',
          isOverbooking: false
        },
        dateStockEntitty: {
          id: '',
          productId: '',
          date: '',
          adultSalePrice: '',
          adultSettlementPrice: '',
          childSalePrice: '',
          childSettlementPrice: '',
          differSalePrice: '',
          differSettlementPrice: '',
          stock: 0,
          saleStock: '',
          isOverbooking: false
        },
        weekStockEntitty: {
          id: '',
          productId: '',
          date: '',
          adultSalePrice: '',
          adultSettlementPrice: '',
          childSalePrice: '',
          childSettlementPrice: '',
          differSalePrice: '',
          differSettlementPrice: '',
          stock: 0,
          saleStock: '',
          isOverbooking: false
        },
        dataLogList: [],
        loginType: '',
        rangeBeginArray: []
      }
    },
    methods: {
      // 禁用没有数据的行
      selectablePop: function (row) {
        if (row.id > 0) {
          return true
        } else {
          return false
        }
      },
      // 选中行
      selsChange: function (sels) {
        this.sels = sels
      },
      handleCheckAllChange (event) {
        this.checkedCities = event.target.checked ? this.week : []
        this.isIndeterminate = false
      },
      handleCheckedCitiesChange (value) {
        let checkedCount = value.length
        this.checkAll = checkedCount === this.week.length
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.week.length
      },
      batchDelete () {
        var ids = []
        this.sels.map(item => {
          if (item.id > 0) {
            ids.push(item.id)
          }
        })
        this.deleteProduct(ids, '')
      },
      // 删除方法
      deleteProduct (ids) {
        this.$confirm('确定删除产品库存吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.$http.post('/smartLight/web/productTourStock/deleteByIds.htm', ids + '|' + this.loginType).then(response => {
            var resBody = response.body
            if (resBody.isSuccess === 0) {
              this.$message({
                showClose: true,
                message: '操作成功',
                type: 'success'
              })
              this.weekNum(this.yearStr, this.monthStr)
            } else if (resBody.isSuccess === 2) {
              this.$alert(resBody.errorMsg, {
                type: 'warning'
              }).then(() => {
                this.$router.push({path: '/login'})
                sessionStorage.clear()
              })
            } else {
              this.$message({
                showClose: true,
                message: '操作失败',
                type: 'error'
              })
            }
          })
        }).catch(() => {
        })
      },
      info () {
        var str = ''
        if (this.timeRange.length <= 0 || this.timeRange[0] < (new Date().getFullYear())) {
          this.$message({
            showClose: true,
            message: '请选择日期范围',
            type: 'error'
          })
          return
        }
        if (this.checkedCities.length <= 0) {
          this.$message({
            showClose: true,
            message: '请选择适用日期',
            type: 'error'
          })
          return
        }
        var dbNumber = this.doubleNum(this.weekStockEntitty)
        if (!dbNumber) {
          return
        }
        var startTime = new Date(this.timeRange[0])
        var endTime = new Date(this.timeRange[1])
        var dateNum = Math.abs((startTime.getTime() - endTime.getTime())) / (1000 * 60 * 60 * 24)
        if (dateNum > 90) {
          this.$message({
            showClose: true,
            message: '日期范围只能为90天（包含）之内',
            type: 'error'
          })
          return
        }
        while ((endTime.getTime() - startTime.getTime()) >= 0) {
          var year = startTime.getFullYear()
          var month = startTime.getMonth().toString().length === 1 ? (startTime.getMonth() + 1).toString() : (startTime.getMonth() + 1)
          var day = startTime.getDate().toString().length === 1 ? startTime.getDate() : startTime.getDate()
          var week = '周' + '日一二三四五六'.split('')[startTime.getDay()]
          for (var i = 0; i < this.checkedCities.length; i++) {
            if (this.checkedCities[i] === week) {
              str += (year + '-' + (month >= 10 ? '' : '0') + (month) + '-' + (day >= 10 ? '' : '0') + day) + ','
            }
          }
          startTime.setDate(startTime.getDate() + 1)
        }
        str = str.substring(0, (str.length - 1))
        this.weekStockEntitty.productId = this.productIdConfig
        var para = {
          dateArray: str,
          dateStockEntitty: this.weekStockEntitty,
          type: this.loginType
        }
        this.saveStockLoading = true
        this.$http.post('/tour/web/productTourStock/saveAll.htm', para).then(response => {
          this.saveStockLoading = false
          var resBody = response.body
          if (resBody.isSuccess === 0) {
            this.resetStock()
            this.weekNum(this.yearStr, this.monthStr)
            this.$message({
              showClose: true,
              message: '编辑价格库存成功',
              type: 'success'
            })
            this.weekNum(this.yearStr, this.monthStr)
          } else if (resBody.isSuccess === 2) {
            this.listLogLoading = false
            this.$alert(resBody.errorMsg, ' ', {
              type: 'warning'
            }).then(() => {
              this.$router.push({path: '/login'})
              sessionStorage.clear()
            })
          } else {
            this.$message({
              showClose: true,
              message: resBody.errorMsg,
              type: 'error'
            })
          }
        })
      },
      oldInventoryNumStr (row, column) {
        if (row.oldInventoryNum === '' && row.id > 0) {
          return '--'
        } else {
          return row.oldInventoryNum
        }
      },
      inventoryNumStr (row, column) {
        if (row.inventoryNum === '' && row.id > 0) {
          return '--'
        } else {
          return row.inventoryNum
        }
      },
      selectM (rangeBegin) { // 展示日期信息
        this.rangeBeginArray = rangeBegin
        this.selPop()
      },
      selectTourDel () {
        if (this.selValue.length > 0) {
          this.$refs.calend.selectDel(this.selValue)
        }
      },
      selPop () { // 加载select文本
        var htmlStr = ''
        for (var i = 0; i < this.rangeBeginArray.length; i++) {
          var days = this.rangeBeginArray[i][2] > 9 ? this.rangeBeginArray[i][2] : ('0' + this.rangeBeginArray[i][2])
          var month = (this.rangeBeginArray[i][1] + 1) > 9 ? (this.rangeBeginArray[i][1] + 1) : ('0' + (this.rangeBeginArray[i][1] + 1))
          var value = this.rangeBeginArray[i][0] + '-' + (this.rangeBeginArray[i][1] + 1) + '-' + this.rangeBeginArray[i][2]
          var text = this.rangeBeginArray[i][0] + '-' + month + '-' + days
          htmlStr += '<option value="' + value + '">' + text + '</option>'
        }
        $('#sel').html(htmlStr)
      },
      testNum (year, month) {
        this.dataList = []
        this.monthStr = month
        // 根据年月获取天数
        var temp = new Date(year, month, 0).getDate()
        // 根据年月获取天数
        for (var i = 1; i <= temp; i++) {
          var week = '周' + '日一二三四五六'.split('')[new Date(Date.UTC(year, month - 1, i)).getDay()]
          var bol = true
          for (var k = 0; k < this.stockList.length; k++) {
            this.dataInfo = {}
            if (this.stockList[k].date === (year + '-' + month + '-' + (i >= 10 ? i : ('0' + i)))) {
              this.dataInfo = {
                id: this.stockList[k].id,
                month: (i + '\t' + week),
                aduMan: '售：' + this.stockList[k].adultSalePrice + '   结：' + this.stockList[k].adultSettlementPrice,
                chdMan: '售：' + this.stockList[k].childSalePrice + '   结：' + this.stockList[k].childSettlementPrice,
                room: '售：' + this.stockList[k].differSalePrice + '   结：' + this.stockList[k].differSettlementPrice,
                inventoryNum: this.stockList[k].stock,
                oldInventoryNum: this.stockList[k].saleStock,
                isBeyond: this.stockList[k].isOverbooking ? '是' : '否'
              }
              this.dataList.push(this.dataInfo)
              bol = false
            }
          }
          if (bol) {
            this.dataInfo = {
              month: (i + '\t' + week),
              aduMan: '',
              chdMan: '',
              room: '',
              inventoryNum: '',
              oldInventoryNum: '',
              isBeyond: ''
            }
            this.dataList.push(this.dataInfo)
          }
        }
      },
      doubleNum (val) {
        if (val.adultSalePrice <= 0) {
          this.$message({
            showClose: true,
            message: '成人销售价需要大于0',
            type: 'error'
          })
          return false
        }
        if (val.adultSettlementPrice <= 0) {
          this.$message({
            showClose: true,
            message: '成人结算价需要大于0',
            type: 'error'
          })
          return false
        }
        if (val.childSalePrice <= 0) {
          this.$message({
            showClose: true,
            message: '儿童销售价需要大于0',
            type: 'error'
          })
          return false
        }
        if (val.childSettlementPrice <= 0) {
          this.$message({
            showClose: true,
            message: '儿童结算价需要大于0',
            type: 'error'
          })
          return false
        }
        if (val.differSalePrice <= 0) {
          this.$message({
            showClose: true,
            message: '单房差销售价需要大于0',
            type: 'error'
          })
          return false
        }
        if (val.differSettlementPrice <= 0) {
          this.$message({
            showClose: true,
            message: '单房差结算价需要大于0',
            type: 'error'
          })
          return false
        }
        var teg = new RegExp('^\\d+.?\\d{0,2}$')
        if (!teg.test(val.adultSalePrice)) {
          this.$message({
            showClose: true,
            message: '成人销售价小数最多两位',
            type: 'error'
          })
          return false
        }
        if (!teg.test(val.adultSettlementPrice)) {
          this.$message({
            showClose: true,
            message: '成人结算价小数最多两位',
            type: 'error'
          })
          return false
        }
        if (!teg.test(val.childSalePrice)) {
          this.$message({
            showClose: true,
            message: '儿童销售价小数最多两位',
            type: 'error'
          })
          return false
        }
        if (!teg.test(val.childSettlementPrice)) {
          this.$message({
            showClose: true,
            message: '儿童结算价小数最多两位',
            type: 'error'
          })
          return false
        }
        if (!teg.test(val.differSalePrice)) {
          this.$message({
            showClose: true,
            message: '单房差销售价小数最多两位',
            type: 'error'
          })
          return false
        }
        if (!teg.test(val.differSettlementPrice)) {
          this.$message({
            showClose: true,
            message: '单房差结算价小数最多两位',
            type: 'error'
          })
          return false
        }
        if (val.stock <= 0) {
          this.$message({
            showClose: true,
            message: '库存数需要大于0',
            type: 'error'
          })
          return false
        }
        var tagStock = new RegExp('^\\d+$')
        if (!tagStock.test(val.stock)) {
          this.$message({
            showClose: true,
            message: '库存数必须为正整数',
            type: 'error'
          })
          return false
        }
        return true
      },
      dateButtonCss (val) {
        this.dateButton = val
      },
      weekNum (year, month) {
        $('#tab_rl .tab_menu li').addClass('others')
        month = Number(month)
        $(('#_' + month)).removeClass('others').addClass('current')
        month = (month >= 10 ? month : ('0' + month))
        var para = {
          productId: this.productIdConfig,
          year_month: (year + '-' + month + '-'),
          type: this.loginType
        }
        this.$http.post('/tour/web/productTourStock/getByCondition.htm', para).then(response => {
          var resBody = response.body
          if (resBody.isSuccess === 0) {
            this.stockList = resBody.result
            this.testNum(year, month)
          } else if (resBody.isSuccess === 2) {
            this.listLogLoading = false
            this.$alert(resBody.errorMsg, ' ', {
              type: 'warning'
            }).then(() => {
              this.$router.push({path: '/login'})
              sessionStorage.clear()
            })
          } else {
            this.$message({
              showClose: true,
              message: resBody.errorMsg,
              type: 'error'
            })
          }
        })
      },
      // 分页插件查询方法-条数
      handleSizeChange (val) {
        this.pageSize = val
        this.initLogList()
      },
      // 分页插件查询方法-页数
      handleCurrentChange (val) {
        this.page = val
        this.currentPage = val
        this.initLogList()
      },
      yearAddSub (val) {
        this.yearStr = this.yearStr + val
        this.weekNum(this.yearStr, this.monthStr)
      },
      handleLog () {
        this.showFormLog = true
        this.dialogLogTitle = '操作日志'
        this.initLogList()
      },
      handleAdd () {
        this.dialogAddTitle = '新增价格/库存'
        this.showFormAdd = true
      },
      handleEditAll () {
        this.dialogAddTitle = '修改库存'
        this.showFormAdd = true
      },
      handleEdit (index, row) {
        if (row.id > 0) {
          this.dialogEditTitle = '编辑库存'
          this.showFormEdit = true
          for (var i = 0; i < this.stockList.length; i++) {
            if (this.stockList[i].id === row.id) {
              this.stockEntitty = {
                id: this.stockList[i].id,
                productId: this.stockList[i].productId,
                date: this.stockList[i].date,
                adultSalePrice: this.stockList[i].adultSalePrice,
                adultSettlementPrice: this.stockList[i].adultSettlementPrice,
                childSalePrice: this.stockList[i].childSalePrice,
                childSettlementPrice: this.stockList[i].childSettlementPrice,
                differSalePrice: this.stockList[i].differSalePrice,
                differSettlementPrice: this.stockList[i].differSettlementPrice,
                stock: this.stockList[i].stock,
                saleStock: this.stockList[i].saleStock,
                isOverbooking: this.stockList[i].isOverbooking
              }
            }
          }
        }
      },
      handSubSave () {
        var dbNumber = this.doubleNum(this.stockEntitty)
        if (!dbNumber) {
          return
        }
        this.stockEntitty.productId = this.productIdConfig
        var para = {
          stockEntitty: this.stockEntitty,
          type: this.loginType
        }
        this.saveStockLoading = true
        this.$http.post('/tour/web/productTourStock/save.htm', para).then(response => {
          this.saveStockLoading = false
          var resBody = response.body
          if (resBody.isSuccess === 0) {
            this.resetStock()
            this.weekNum(this.yearStr, this.monthStr)
            this.$message({
              showClose: true,
              message: '编辑价格库存成功',
              type: 'success'
            })
          } else if (resBody.isSuccess === 2) {
            this.listLogLoading = false
            this.$alert(resBody.errorMsg, ' ', {
              type: 'warning'
            }).then(() => {
              this.$router.push({path: '/login'})
              sessionStorage.clear()
            })
          } else {
            this.$message({
              showClose: true,
              message: resBody.errorMsg,
              type: 'error'
            })
          }
        })
      },
      handSubSaveMoreDate () {
        if (this.rangeBeginArray.length <= 0) {
          this.$message({
            showClose: true,
            message: '请选择日期',
            type: 'error'
          })
          return
        }
        if (this.rangeBeginArray.length > 90) {
          this.$message({
            showClose: true,
            message: '只能选择90天（包含）',
            type: 'error'
          })
          return
        }
        var dbNumber = this.doubleNum(this.dateStockEntitty)
        if (!dbNumber) {
          return
        }
        var str = ''
        for (var i = 0; i < this.rangeBeginArray.length; i++) {
          str += this.rangeBeginArray[i][0] + '-' + ((this.rangeBeginArray[i][1] + 1) >= 10 ? '' : '0') + (this.rangeBeginArray[i][1] + 1) + '-' + ((this.rangeBeginArray[i][2]) >= 10 ? '' : '0') + this.rangeBeginArray[i][2] + ','
        }
        if (str !== '') {
          str = str.substring(0, (str.length - 1))
        }
        this.dateStockEntitty.productId = this.productIdConfig
        var para = {
          dateArray: str,
          dateStockEntitty: this.dateStockEntitty,
          type: this.loginType
        }
        this.saveStockLoading = true
        this.$http.post('/tour/web/productTourStock/saveAll.htm', para).then(response => {
          this.saveStockLoading = false
          var resBody = response.body
          if (resBody.isSuccess === 0) {
            this.resetStock()
            this.weekNum(this.yearStr, this.monthStr)
            this.$message({
              showClose: true,
              message: '编辑价格库存成功',
              type: 'success'
            })
          } else if (resBody.isSuccess === 2) {
            this.listLogLoading = false
            this.$alert(resBody.errorMsg, ' ', {
              type: 'warning'
            }).then(() => {
              this.$router.push({path: '/login'})
              sessionStorage.clear()
            })
          } else {
            this.$message({
              showClose: true,
              message: resBody.errorMsg,
              type: 'error'
            })
          }
        })
      },
      resetStock () {
        this.dateButton = false
        this.showFormEdit = false
        this.showFormAdd = false
        this.stockEntitty = {
          id: '',
          productId: '',
          date: '',
          adultSalePrice: 0,
          adultSettlementPrice: 0,
          childSalePrice: 0,
          childSettlementPrice: 0,
          differSalePrice: 0,
          differSettlementPrice: 0,
          stock: 0,
          saleStock: '',
          isOverbooking: false
        }
        this.dateStockEntitty = {
          id: '',
          productId: '',
          date: '',
          adultSalePrice: 0,
          adultSettlementPrice: 0,
          childSalePrice: 0,
          childSettlementPrice: 0,
          differSalePrice: 0,
          differSettlementPrice: 0,
          stock: 0,
          saleStock: '',
          isOverbooking: false
        }
        this.weekStockEntitty = {
          id: '',
          productId: '',
          date: '',
          adultSalePrice: 0,
          adultSettlementPrice: 0,
          childSalePrice: 0,
          childSettlementPrice: 0,
          differSalePrice: 0,
          differSettlementPrice: 0,
          stock: 0,
          saleStock: '',
          isOverbooking: false
        }
        this.rangeBeginArray = []
      },
      // 初始化分页查询常旅客信息
      initLogList () {
        this.listLogLoading = true
        var para = {
          pageNumber: this.page,
          pageSize: this.pageSize,
          productId: this.productIdConfig,
          type: this.loginType
        }
        this.$http.post('/tour/web/productLog/getList.htm', para).then(response => {
          var resBody = response.body
          if (resBody.isSuccess === 0) {
            this.total = resBody.result.totalElements
            this.dataLogList = resBody.result.content
            this.listLogLoading = false
          } else if (resBody.isSuccess === 2) {
            this.listLogLoading = false
            this.$alert(resBody.errorMsg, ' ', {
              type: 'warning'
            }).then(() => {
              this.$router.push({path: '/login'})
              sessionStorage.clear()
            })
          } else {
            this.$message({
              showClose: true,
              message: resBody.errorMsg,
              type: 'error'
            })
          }
        })
      },
      //      时间格式化
      createTimeStr (row, column) {
        var d = new Date(row.createTime)
        var year = d.getFullYear()
        var month = d.getMonth() + 1
        if (month < 10) {
          month = '0' + month
        }
        var date = d.getDate()
        if (date < 10) {
          date = '0' + date
        }
        var hh = d.getHours()
        if (hh < 10) {
          hh = '0' + hh
        }
        var mm = d.getMinutes()
        if (mm < 10) {
          mm = '0' + mm
        }
        var ss = d.getSeconds()
        if (ss < 10) {
          ss = '0' + ss
        }
        return year + '-' + month + '-' + date + ' ' + hh + ':' + mm + ':' + ss
      }
    },
    mounted () {
      var user = sessionStorage.getItem('user')
      if (user) {
        user = JSON.parse(user)
        this.loginType = user.type
      }
      this.monthStr = (new Date().getMonth() + 1)
      this.yearStr = (new Date().getFullYear())
      this.weekNum(this.yearStr, (new Date().getMonth() + 1))
    },
    components: {
      calendar
    }
  }
</script>
<style lang="stylus" rel="stylesheet/stylus">
  #product
    .el-dialog--small
      width: 700px !important

    .inputcss {
      height: 36px
      line-height: 40px
      width: 150px
      border: 0px
      border-radius: 0px
      padding-left: 0px
      margin-left: 0px
    }

    .el-picker-panel
      .el-date-range-picker
        width: 220px
        transform-origin: center top 0px
        z-index: 2014
        position: fixed
        top: 124px
        left: 372px

    .flex {
      box-sizing: border-box
      display: -webkit-box
      -webkit-box-pack: start
      -webkit-box-align: start
      display: -webkit-flex
      -webkit-justify-content: space-between
      -webkit-align-items: top
      display: flex
      justify-content: space-between
      align-items: top
      flex-flow: row wrap
    }

    .flex > div {
      margin: 0px
      padding: 0px
      width: 25%
      min-width: 300px
      border: 1px solid #eee
      border-radius: 2px
      position: relative
    }

    .flex > div > span {
      position: absolute
      left: 0px
      top: 0px
      padding: 2px 10px
      font-size: 10px
      border-radius: 0 0 2px 0
      background: #ea6151
      color: #fff
    }

    .selDate {
      width: 200px;
      line-height: 30px;
      border: 1px solid #ccc;
      height: 180px;
      padding: 5px 0;
    }

    select:-internal-list-box {
      -webkit-appearance: listbox;
      align-items: flex-start;
      overflow-x: hidden;
      overflow-y: scroll;
      vertical-align: text-bottom;
      user-select: none;
      white-space: nowrap;
      border-width: 1px;
      border-style: inset;
      border-color: grey;
      border-image: initial;
      border-radius: initial;
    }
    .el-input__inner
      height: 25px !important

    .el-dialog__body {
      padding: 0px 20px;
    }
</style>
